<?php

class SiteController extends CController
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image
			// this is used by the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xEBF4FB,
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		if (Yii::app()->params['allowSimulation'] === true && Yii::app()->user->getState('dbuser')->simulation){
			$this->redirect(array('/simulation'));
			return;
		}
		
		if (!Yii::app()->user->isGuest)
			$this->render('index_'.Yii::app()->user->getLanguage());
		else
			$this->actionLogin();
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$this->layout = 'main';
		$form=new LoginForm;
		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$form->attributes=$_POST['LoginForm'];
			// validate user input and redirect to previous page if valid
			if($form->validate()) {
				if (Yii::app()->params['allowSimulation'] === true && Yii::app()->user->getState('dbuser')->simulation)
					$this->redirect(array('/simulation'));
				else
					$this->redirect(Yii::app()->user->returnUrl);
			}
		}
		// display the login form
		$this->render('login',array('form'=>$form));
	}

	/**
	 * Logout the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		$this->layout = 'main';
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionAdmin()
	{
		$this->layout = 'admin';
		$this->render('index');
	}
	
	public function actionTest()
	{
		$className=Yii::import("application.extensions.template.ReportTemplate",true);
		
		$m = Meetings::model()->findByPk(1);
		$p = $m->project;
		
		$tpl = new $className(dirname(__FILE__).DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."templates", "keep");
		
		$tpl->set_file("edition", "situation_projet.htm");
		
		// Add meeting information
		$this->setAttributeVar($tpl,'meeting', $m);
		
		// Add project information
		$this->setAttributeVar($tpl,'project', $p);
		$tpl->set_var("project.statistics_graph", "<img src='index.php?r=adviser/project/drawGraph&id=".$p->id."' />");
		
		// Add owner information
		$this->setAttributeVar($tpl,'owner', $p->owner);
		$tpl->set_var("owner.full_name", $p->owner->FullName);
		
		// Add route information
		$this->setAttributeVar($tpl,'route', $p->route);

		// Add city information
		$this->setAttributeVar($tpl,'city', $p->owner->city);
		$tpl->set_var("city.country", $p->owner->city->country->country_name);
		
		// Add adviser information
		$this->setAttributeVar($tpl,'adviser', $p->owner->adviser);
		$tpl->set_var("adviser.full_name", $p->owner->adviser->FullName);
		
		// Add all topics informations
		$tpl->set_block("edition", "all.topics", "all_topics");
		foreach ($p->topics as $topic)
		{
			$this->setAttributeVar($tpl,'topic', $topic);
			$this->setAttributeVar($tpl,'topic', $topic->topic);
			$tpl->parse("all_topics", "all.topics", true);
		}
		
		// Add custom information
		$tpl->set_var("today", date(Constants::SQL_DATE_FORMAT));
		
		
		
		$tpl->parse("OUT", "edition");
		
		$tpl->p("OUT");
	}
	
	private function setAttributeVar($tpl, $prefix, $object)
	{
		foreach ($object->attributes as $attribute=>$value)
		{
			$tpl->set_var($prefix.".".$attribute, $value);
		}
	}
	
	private function parse($tpl, $block, $prefix, $object)
	{
		$v = "";
		foreach ($object->attributes as $attribute=>$value)
		{
			$tpl->parse($v, $block, true);
			$tpl->set_var($prefix.".".$attribute, $value);
		}
	}
}